package com.setup.trac.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.hibernate.FlushMode;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateAccessor;
import org.springframework.orm.hibernate3.HibernateTemplate;

public class DAO  {

	
	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}


	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
		this.hibernateTemplate.setFlushMode(HibernateAccessor.FLUSH_ALWAYS);
	}


	private HibernateTemplate hibernateTemplate;

	/* (non-Javadoc)
	 * @see org.cow.core.dao.DAO#insert(java.lang.Object)
	 */
	public <T>T insert(T persistentObject){
		getHibernateTemplate().persist(persistentObject);
		return persistentObject;
	}
	
	public Object[] insertAll(Object[] persistentObjects){
		for(int i=0;i<persistentObjects.length;i++){
			getHibernateTemplate().persist(persistentObjects[i]);
		}
		return persistentObjects;
	}


	
	public boolean delete(Object persistentObject) {
		try {
			getHibernateTemplate().delete(persistentObject);
			return true;
		} catch (DataAccessException e) {
			return false;
		}
	}


	
	public boolean deleteAll(Object[] persistentObjects) {
		try {
			List<?> all = new ArrayList<Object>();
			CollectionUtils.addAll(all, persistentObjects);
			getHibernateTemplate().deleteAll(all);
			return true;
		} catch (DataAccessException e) {
			return false;
		}
	}


	


	
	public Object update(Object persistentObject) {
		// TODO Auto-generated method stub
		return null;
	}


	
	public Object[] updateAll(Object[] persistentObjects) {
		// TODO Auto-generated method stub
		return null;
	}


	
}
